
from subprocess import call
from os.path import splitext
import os
import sys

def gen_single_wave(mod, prefix, filter, led_filter, subsong = -1):
    
    wave = splitext(mod)[0];
    
    if (subsong == -1):
        subsong = '';
    else:
        subsong = '--subsong={0}'.format(subsong)
	
    data_dir = os.path.dirname(os.path.dirname(os.getcwd())).replace('\\', '//') + '//samples//mods'
	
    mod_in = '{0}//{1}'.format(data_dir, mod)
    wave_out = '{0}//{1} - {2}.wav'.format(data_dir, wave, prefix)
    txt_out = '{0}//{1} - {2}.txt'.format(data_dir, wave, prefix)
    txt_in = '{0}//{1}.txt'.format(data_dir, os.path.splitext(mod)[0])

    str = 'uade123.exe --filter={0} --scope --force-led={1} {2} --resampler=sinc -f "{3}" "{4}"'.format(
	     filter, led_filter, subsong, wave_out, mod_in)
    
    call(str);
	
    os.rename(txt_in, txt_out)

def gen_waves_groups(mod, subsong = -1):
    gen_single_wave(mod, 'a500 led filter off', 'a500', 0, subsong);
    gen_single_wave(mod, 'a500 led filter on', 'a500', 1, subsong);
    gen_single_wave(mod, 'a1200 led filter off', 'a1200', 0, subsong);
    gen_single_wave(mod, 'a1200 led filter on', 'a1200', 1, subsong);
    gen_single_wave(mod, 'a500 filter off', 'none', 0, subsong);
    gen_single_wave(mod, 'a1200 filter off', 'none', 0, subsong);
    
def gen_waves():
    gen_waves_groups('Alien Breed - Main Theme.bp')
    gen_waves_groups('Body Blows Galactic - Main Theme.p4x')
    gen_waves_groups('Imploder4.mso', 8)
    gen_waves_groups('Lemmings 2 - Main Theme.med')
    gen_waves_groups('Stardust - Main Theme.p4x')

if __name__ == '__main__':
    os.chdir('..//..//uade 2.13//bin')
    gen_waves()